package com.qq2008.game.mojie.interceptor;

import com.qq2008.game.mojie.jdo.Role;
import com.qq2008.game.mojie.server.RoleServer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

// 魔界状态校验校验")
public class MoStateCheckInterceptor extends HandlerInterceptorAdapter {

    // 不用拦截的路径
    private final String excludePath[] = {"server", "main", "createRole", "login", "battle", "showBattle", "battleFlee"};

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String cmd = request.getParameter("cmd");
        if (!Arrays.asList(excludePath).contains(cmd)) {
            HttpSession session = request.getSession();
            Object object = session.getAttribute("roleId");
            int roleId = (int) object;
            Role role = RoleServer.getRole(roleId);
            if (null != role.getBattle()) {
                response.sendRedirect("/mojie/game?cmd=showBattle");
                return false;
            }
        }
        return true;
    }

}
